package com.hdd.biz.painting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.biz.painting.entity.FileInfo;
import com.hdd.biz.painting.entity.PaintingTopicLink;
import com.hdd.biz.painting.mapper.PaintingTopicLinkMapper;
import com.hdd.biz.painting.service.PaintingTopicLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.dto.SuperResponse;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-04-25
 */
@Service
public class PaintingTopicLinkServiceImpl extends ServiceImpl<PaintingTopicLinkMapper, PaintingTopicLink> implements PaintingTopicLinkService {


    @Override
    public void delTopicLinksBy(String worksId) {
        QueryWrapper<PaintingTopicLink> wrapper = new QueryWrapper<>();
        wrapper.eq("works_id",worksId);
        baseMapper.delete(wrapper);
    }

    @Override
    public void insert(String worksId, String id) {
        PaintingTopicLink link = new PaintingTopicLink();
        link.setTopicId(id);
        link.setWorksId(worksId);

        QueryWrapper<PaintingTopicLink> wrapper = new QueryWrapper<>();
        wrapper.eq("works_id",worksId);
        wrapper.eq("topic_id",id);
        if(baseMapper.selectCount(wrapper)<=0){
            baseMapper.insert(link);
        }
    }

    @Override
    public void delTopic(String worksId, String id) {
        QueryWrapper<PaintingTopicLink> wrapper = new QueryWrapper<>();
        wrapper.eq("works_id",worksId);
        wrapper.eq("topic_id",id);
        baseMapper.delete(wrapper);
    }

    @Override
    public List<String> getids(String worksId) {
        return baseMapper.getTopicIds(worksId);
    }

    @Override
    public List<String> getWorksId(String topicId) {
        return baseMapper.getWorkIds(topicId);
    }

    @Override
    public List<String> getWorksId(String topicId, Integer pageNo, Integer pageSize) {
        List<String> list = new ArrayList<>();
        QueryWrapper<PaintingTopicLink> wrapper = new QueryWrapper();
        wrapper.eq("topic_id",topicId);
        Page<PaintingTopicLink> pages = new Page<>(pageNo,pageSize);
        IPage<PaintingTopicLink> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingTopicLink> agents = mapIPage.getRecords();
        for(int i =0;i<agents.size();i++){
            list.add(agents.get(i).getWorksId());
        }
        return list;
    }
}
